using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RUPS
{
    public partial class A_Evidenta_tipuri_materiale : Form
    {
        public int IdO;
        public string numeMatO;
        public int nivMinO;
        public int idCulO;
        public int dimO;
        SqlConnection conexiune;

        public A_Evidenta_tipuri_materiale()
        {
            InitializeComponent();
        }

        private void A_Evidenta_stocuri_Load(object sender, EventArgs e)
        {
            try
            {
                SqlDataAdapter adaptor = new SqlDataAdapter("select id, numeCuloare from culori", tipMaterialTableAdapter.Connection);
                DataSet dataset = new DataSet();
                adaptor.Fill(dataset, "tip");
                a_culoare.DataSource = dataset.Tables["tip"];
                a_culoare.DisplayMember = dataset.Tables["tip"].Columns["numeCuloare"].ToString();
                a_culoare.ValueMember = dataset.Tables["tip"].Columns["id"].ToString();
                a_culoare.Text = "";

                // TODO: This line of code loads data into the 'tipMaterialDataSet.tipMaterial' table. You can move, or remove it, as needed.
                this.tipMaterialTableAdapter.Fill(this.tipMaterialDataSet.tipMaterial);
                // TODO: This line of code loads data into the 'tipMaterialDS.tipMaterial' table. You can move, or remove it, as needed.
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            

        }

        private void adaugTipMat_Click(object sender, EventArgs e)
        {
            try
            {
                if (a_denMat.Text == "")
                    throw new InvalidOperationException("atentie la datele obligatorii");
                if (a_nivMin.Text == "")
                    throw new InvalidOperationException("atentie la datele obligatorii");
                if (a_culoare.SelectedValue.ToString() == "")
                    throw new InvalidOperationException("atentie la datele obligatorii");
                if (a_dimMat.Text == "")
                    throw new InvalidOperationException("atentie la datele obligatorii");

                tipMaterialTableAdapter.Insert(a_denMat.Text, int.Parse(a_nivMin.Text.ToString()), int.Parse(a_culoare.SelectedValue.ToString()), int.Parse(a_dimMat.Text.ToString()));
                gridTipMateriale.DataSource = null;
                gridTipMateriale.Rows.Clear();
                SqlDataAdapter da = new SqlDataAdapter("select * from tipMaterial", tipMaterialTableAdapter.Connection);
                DataSet ds = new DataSet();
                da.Fill(ds, "tipMat");

                gridTipMateriale.DataSource = ds.Tables["tipMat"];
            }
            catch (InvalidOperationException inv)
            {
                MessageBox.Show(inv.Message, "atentie", MessageBoxButtons.OK, MessageBoxIcon.Warning);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void gridTipMateriale_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
            DataGridViewRow rand = gridTipMateriale.SelectedRows[0];
            a_denMat.Text = rand.Cells[1].Value.ToString();
            numeMatO = a_denMat.Text.ToString();
            a_dimMat.Text = rand.Cells[4].Value.ToString();
            dimO = int.Parse(a_dimMat.Text.ToString());
            a_nivMin.Text = rand.Cells[2].Value.ToString();
            nivMinO = int.Parse(a_nivMin.Text.ToString());
            IdO = int.Parse(rand.Cells[0].Value.ToString());
             conexiune = tipMaterialTableAdapter.Connection;
            conexiune.Open();
           SqlCommand comanda = new SqlCommand("denumireCuloare", conexiune);
                SqlParameter idCul, denCul;
                idCul = new SqlParameter("@id", SqlDbType.Int);
                idCul.Direction = ParameterDirection.Input;
                //idMatO = int.Parse(rand.Cells[4].Value.ToString());
                idCul.Value = int.Parse(rand.Cells[3].Value.ToString());
                idCulO =int.Parse( idCul.Value.ToString());
                comanda.Parameters.Add(idCul);

                denCul  = new SqlParameter("@numeCuloare", SqlDbType.VarChar);
                denCul.Size = 50;
                denCul.Direction = ParameterDirection.Output;
                comanda.Parameters.Add(denCul);

                comanda.CommandType = CommandType.StoredProcedure;
                comanda.ExecuteNonQuery();

                string denCuloare = comanda.Parameters[1].Value.ToString();
                int idxCul = a_culoare.FindStringExact(denCuloare);
                a_culoare.SelectedIndex = idxCul;

                conexiune.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                conexiune.Close();
               
            }
          
        }

        private void a_nivMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == ',')
                base.OnKeyPress(e);
            else
                e.Handled = true;
        }

        private void a_dimMat_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == ',')
                base.OnKeyPress(e);
            else
                e.Handled = true;
        }

        private void a_revoca_Click(object sender, EventArgs e)
        {
            a_culoare.Text = "";
            a_denMat.Text = "";
            a_dimMat.Text = "";
            a_nivMin.Text = "";
        }

        private void sterge_Click(object sender, EventArgs e)
        {
            try{
                if (a_denMat.Text == "")
                    throw new InvalidOperationException("atentie la datele introduse");
                if (a_dimMat.Text == "")
                    throw new InvalidOperationException("atentie la datele introduse");
                if (a_culoare.SelectedValue.ToString() == "")
                    throw new InvalidOperationException("atentie la datele introduse");
                if (a_nivMin.Text == "")
                    throw new InvalidOperationException("atentie la datele introduse");

                if (MessageBox.Show("Stergeti materialul?", "Confirmare", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    try
                    {
                        SqlConnection conexiune = tipMaterialTableAdapter.Connection;
                        conexiune.Open();
                        SqlCommand comanda = new SqlCommand("delete from tipMaterial where id = " + IdO, conexiune);
                        comanda.ExecuteNonQuery();
                        tipMaterialDataSetTableAdapters.tipMaterialTableAdapter tac = new RUPS.tipMaterialDataSetTableAdapters.tipMaterialTableAdapter();


                        tipMaterialBindingSource.ResetBindings(false);
                        gridTipMateriale.DataSource = null;


                        tipMaterialDataSet.tipMaterialDataTable dt = new tipMaterialDataSet.tipMaterialDataTable();
                        tac.Fill(dt);
                        tipMaterialBindingSource.DataSource = dt;
                        gridTipMateriale.DataSource = tipMaterialBindingSource;
                        gridTipMateriale.Invalidate();
                        conexiune.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            catch(InvalidOperationException inv)
            {
                MessageBox.Show(inv.Message, "atentie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            }


        private void a_modifMat_Click(object sender, EventArgs e)
        {
            tipMaterialDataSetTableAdapters.tipMaterialTableAdapter tac = new RUPS.tipMaterialDataSetTableAdapters.tipMaterialTableAdapter();
            try
            {
                if (a_denMat.Text == "")
                    throw new InvalidOperationException("atentie la datele introduse");
                if(a_dimMat.Text == "")
                    throw new InvalidOperationException("atentie la datele introduse");
                if(a_culoare.SelectedValue.ToString() == "")
                    throw new InvalidOperationException("atentie la datele introduse");
                if(a_nivMin.Text == "")
                    throw new InvalidOperationException("atentie la datele introduse");
                
                tac.UpdateTipMaterial(a_denMat.Text.ToString(), int.Parse(a_nivMin.Text.ToString()),
                    int.Parse(a_culoare.SelectedValue.ToString()), int.Parse(a_dimMat.Text.ToString()),
                    IdO, numeMatO, nivMinO, idCulO, dimO);


                tipMaterialBindingSource.ResetBindings(false);
                gridTipMateriale.DataSource = null;
                tipMaterialDataSet.tipMaterialDataTable dt = new tipMaterialDataSet.tipMaterialDataTable();
                tac.Fill(dt);
                tipMaterialBindingSource.DataSource = dt;

                gridTipMateriale.DataSource = tipMaterialBindingSource;
                gridTipMateriale.Invalidate();
             
            }
            catch (InvalidOperationException op)
            {
                MessageBox.Show(op.Message);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

      
     
        

        
    }
}